package com.xzz;

/**
 * @author: hhz
 * @create: 2021-12-09 16:51
 **/
public class Rotate {
    public static void main(String[] args) {
        int[][] matrix = new int[][]{{1,2,3},{4,5,6},{7,8,9}};
        printNums(matrix);
        rotate(matrix);
        System.out.println();
        printNums(matrix);
    }
    public static void rotate(int[][] matrix) {
        int row = matrix.length;
        //水平翻转
        for(int i = 0 ;i<row/2;i++){
            for(int j=0;j<matrix[i].length;j++){
                int temp = matrix[i][j];
                matrix[i][j] = matrix[row-i-1][j];
                matrix[row-i-1][j] = temp;
            }
        }
        //主对角线翻转
        for(int i = 0 ;i<row;i++){
            for (int j= 0;j<i;j++){
                int temp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = temp;
            }
        }
    }

    public static void printNums(int[][] matrix) {
        for (int[] ints : matrix) {
            for (int anInt : ints) {
                System.out.print(anInt+" ");
            }
            System.out.println();
        }
    }
}
